/*
_public.h中的函数实现
*/

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include "_public.h"

void init_queue(Queue *pqueue)
{
    pqueue->head=(D_Node *)malloc(sizeof(Node));
    pqueue->tail=(D_Node *)malloc(sizeof(Node));


    pqueue->head->next=pqueue->tail;
    pqueue->tail->prev=pqueue->head;

    pqueue->head->prev=NULL;
    pqueue->tail->next=NULL;

    pqueue->size=0;
}

void init_stack(Stack *pstack)
{
    pstack->top=(Node *)malloc(sizeof(Node));
    pstack->top->next=NULL;
    pstack->size=0;
}

bool is_empty_stack(Stack *pstack)
{
    return pstack->top->next==NULL;
}

bool is_legal_input(char *string)
{
    while(*string)
    {
        if(*string=='/')
            break;
        string++;
    }
    if(*(++string)=='0')
        return false;
    else
        return true;
    return true;
}

int push(Stack *pstack, ElementType e)
{
    Node *n=(Node *)malloc(sizeof(Node));
    if(n==NULL || pstack==NULL)
        return 0;
    memcpy(&n->ch,&e,sizeof(ElementType));

    if(is_empty_stack(pstack))
    {
        pstack->top->next=n;
        n->next=NULL;
        pstack->size++;
    }
    else
    {
        Node *t=pstack->top->next;
        pstack->top->next=n;

        n->next=t;
        pstack->size++;
    }
    return 1;

}


int push_num(Stack *pstack, int e)
{
    Node *n=(Node *)malloc(sizeof(Node));
    if(n==NULL || pstack==NULL)
        return 0;
    memcpy(&n->num,&e,sizeof(int));

    if(is_empty_stack(pstack))
    {
        pstack->top->next=n;
        n->next=NULL;
        pstack->size++;
    }
    else
    {
        Node *t=pstack->top->next;
        pstack->top->next=n;

        n->next=t;
        pstack->size++;
    }
    return 1;
}

ElementType pop(Stack *pstack)
{
    ElementType e;
    ElementType special='?';
    if(is_empty_stack(pstack))
    {
        printf("这是个空栈\n");
        return special;
    }

    Node *t=pstack->top->next;
    memcpy(&e,&t->ch,sizeof(ElementType));

    pstack->top->next=t->next;

    free(t);
    t->next=NULL;

    // printf("%c-",e);
    return e;

}

int pop_num(Stack *pstack)
{
    int e;
    // ElementType special='?';
    
    Node *t=pstack->top->next;
    memcpy(&e,&t->num,sizeof(int));

    pstack->top->next=t->next;

    free(t);
    t->next=NULL;

    // printf("%c-",e);
    return e;
}


int add(Queue *pqueue, ElementType e)
{
    D_Node *n=(D_Node *)malloc(sizeof(Node));
    if(n==NULL || pqueue==NULL)
        return 0;
    n->data.symblo=e;

    if(pqueue->size==0)
    {
        pqueue->head->next=n;
        n->prev=pqueue->head;

        pqueue->tail->prev=n;
        n->next=pqueue->tail;

        pqueue->size++;
    }
    else
    {
        D_Node *t=pqueue->tail->prev;
        
        t->next=n;
        n->prev=t;

        n->next=pqueue->tail;
        pqueue->tail->prev=n;

        pqueue->size++;
    }
    n->data.Is_num=false;
    return 1;
}

int add_numbers(Queue *pqueue, int e)
{
    D_Node *n=(D_Node *)malloc(sizeof(Node));
    if(n==NULL || pqueue==NULL)
        return 0;
    n->data.num=e;
    if(pqueue->size==0)
    {
        pqueue->head->next=n;
        n->prev=pqueue->head;

        pqueue->tail->prev=n;
        n->next=pqueue->tail;

        pqueue->size++;
    }
    else
    {
        D_Node *t=pqueue->tail->prev;
        
        t->next=n;
        n->prev=t;

        n->next=pqueue->tail;
        pqueue->tail->prev=n;

        pqueue->size++;
    }
    n->data.Is_num=true;
    return 1;
}



void get_element(Queue *pqueue)
{
    D_Node *n=pqueue->head->next;

    pqueue->head->next=n->next;
    n->next->prev=pqueue->head;

    free(n);
    n->next=NULL;
    n->prev=NULL;
    pqueue->size--;
}


void delete_space(char *str, int len)
{
    char temp[len];
    memset(temp,0,sizeof(temp));
    int j=0;
    for(int i=0;i<len;i++)
    {
        if(!isspace(str[i]))
        {
            temp[j]=str[i];
            j++;
        }
    }

    for(int i=0;i<j;i++)
    {
        str[i]=temp[i];
    }
    str[j]='\0';
}

void dispay(Queue *pqueue)
{
    D_Node *t=pqueue->head->next;
    while(t!=pqueue->tail)
    {
        if(t->data.Is_num)
            printf("%d ",t->data.num);
        else
            printf("%c ",t->data.symblo);
        t=t->next;
    }
    printf("\n");
}

bool priority(Stack *pstack, ElementType e)
{
    if(is_empty_stack(pstack))
        return false;
    if((e=='-' || e=='+'))
        return  true;
    ElementType t=pstack->top->next->ch;        // 栈顶元素
    if(!is_low(e) && is_low(t))
        return false;
    else
        return true;
}

bool is_low(ElementType e)
{
    if(e=='+' || e=='-')
        return true;
    else
        return false;
}


bool Is_left_bracket(Stack *pstack)
{
    Node *n=pstack->top->next;
    // printf("aaa=%c\n",n->ch);
    if(n->ch=='(')
        return true;
    else
        return false;
}


void dispaly_stack(Stack *pstack)
{
    Node *t=pstack->top->next;
    while (t!=NULL)
    {
        printf("%c ",t->ch);
        t=t->next;
    }
    putchar('\n');
}


void A_init_stack(A_Stack *pstack)
{
    pstack->top=-1;     // 这时栈为空
}

bool A_is_empty_stack(A_Stack *pstack)
{
    if(pstack->top==-1)
        return true;
    else
        return false;
}

int A_push(A_Stack *pstack, ElementType e)
{
    if(pstack==NULL)
        return 0;
    pstack->top++;
    pstack->data[pstack->top]=e;
    return 1;
}

ElementType A_pop(A_Stack *pstack)
{
    char t=pstack->data[pstack->top];
    pstack->top--;
    return t;
}

void A_dispaly_s(A_Stack *pstack)
{
    for(int i=pstack->top;i>=0;i--)
    {
        printf("%c ",pstack->data[i]);
    }
    putchar('\n');
}

void A_Clear(A_Stack *pstack)
{
    memset(&pstack->data,0,sizeof(char)*MAXSIZE);
    pstack->top=-1;
}

void A_pop_all(A_Stack *pstack,Queue *pqueue)
{
    while(pstack->data[pstack->top]!='(')
    {
        char ch=A_pop(pstack);
        add(pqueue,ch);
    }
    A_pop(pstack);
}

void fun(A_Stack *pstack,Queue *pqueue,char ch)
{
    if(A_is_empty_stack(pstack))
        A_push(pstack,ch);
    else
    {
        while(A_priority(pstack,ch) && pstack->data[pstack->top]!='(')
        {
            char cc=A_pop(pstack);
            add(pqueue,cc);
        }
        A_push(pstack,ch);      // 将左括号出栈
    }
}


bool A_priority(A_Stack *pstack, ElementType e)
{
    if(A_is_empty_stack(pstack))
        return false;
    if((e=='-' || e=='+'))
        return  true;
    char ch=pstack->data[pstack->top];
    if(!is_low(e) && is_low(ch))
        return false;
    else
        return true;
}